package com.flink.api.unit9;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * union_stream_1
 *
 * @author caizhiyang
 * @since 2024-02-23
 */
public class union_stream_2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> ds1 = env.socketTextStream("192.168.226.130", 7777);
        SingleOutputStreamOperator<Integer> ds2 = env.socketTextStream("192.168.226.130", 8888).map(Integer::valueOf);

        /**
         * 使用 connect 合流
         * 1.一次只能连接2条流
         * 2.流的数据类型可以不一致
         * 3.连接后可以调用map,flatMap,filter来处理,但各处理各的
         */
        ConnectedStreams<String, Integer> connect = ds1.connect(ds2);

        connect.map(new CoMapFunction<String, Integer, Integer>() {
            @Override
            public Integer map1(String s) throws Exception {
                return Integer.valueOf(s);
            }

            @Override
            public Integer map2(Integer integer) throws Exception {
                return integer;
            }
        }).print();
        env.execute();
    }
}
